<template>
  <div class="educationadd">
    <div v-show="enmopcityshow">
      <div class="home">
        <div class="navheaders">
          <van-nav-bar @click-left="back">
            <template #left>
              <van-icon name="arrow-left" color="#fff" size="0.6rem" />
            </template>
            <template #title>
              <span>
                <img src="../../assets/img/logo.png" />
              </span>
            </template>
            <!-- <template #right>
            <van-icon name="wap-nav" color="#fff" size="0.6rem" />
          </template>-->
          </van-nav-bar>
        </div>
      </div>

      <div class="myumeremove">
        <div class="educationaddchild">
          <div class="educationaddchildcont">
            <p class="educationaddlift">失业人员</p>
          </div>
          <van-divider color="#E8E8E8" />
          <van-form @submit="onSubmit" label-align="right">
            <van-field v-model="formdata.name" name="username" label="姓名" placeholder="请填写姓名" :rules="[{ required: true, message: ' ' },
                    { pattern: /^[^\s]*$/, message: ' '}]" />
            <van-field v-model="formdata.idCard" :maxlength="18" name="idcard" label="身份证号" placeholder="请填写身份证号"
              :rules="[{ required: true, message: ' ' },
                    { pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: ' '}]" />

            <van-field readonly clickable name="sex" :value="formdata.sex" label="性别" placeholder="请选择性别"
              @click="emplouxb = true" :rules="[{ required: true, message: ' ' }]" />
            <van-popup v-model="emplouxb" position="bottom">
              <van-picker show-toolbar value-key="name" :columns="columnsxb" :item-height="45" @confirm="onConfirmxb"
                @cancel="emplouxb = false" />
            </van-popup>
            <van-field readonly clickable name="domicilePlace" :value="formdata.domicilePlace" label="户籍地"
              placeholder="请选择户籍地" @click="emplouxbhjd(1)" />
            <van-field v-model="formdata.domicilePlaceAddress" name="domicilePlaceAddress" label="户籍地地址"
              placeholder="请填写户籍地地址" :rules="[{ required: true, message: ' ' },
                    { pattern: /^[^\s]*$/, message: ' '}]" />
            <van-field readonly clickable name="currentResidence" :value="formdata.currentResidence" label="现居住地"
              placeholder="请选择现居地" @click="emplouxbhjd(2)" :rules="[{ required: true, message: ' ' }]" />
            <van-field v-model="formdata.currentResidenceAddress" name="currentResidenceAddress" label="现居住地地址"
              placeholder="请填写现居地地址" :rules="[{ required: true, message: ' ' },
                    { pattern: /^[^\s]*$/, message: ' '}]" />
            <van-field v-model="formdata.workExp" name="workExp" label="工作经验" type="digit" placeholder="请输入工作经验" :rules="[{ required: true, message: ' ' },
           ]" />
            <van-field v-model="formdata.phone" name="phone" label="联系电话" type="digit" placeholder="请输入联系电话" :rules="[{ required: true, message: ' ' },
           { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误！'}]" />
            <van-field v-model="formdata.mail" name="mail" label="邮箱" placeholder="请输入邮箱" :rules="[{ required: false, message: ' ' },
           ]" />
            <van-field readonly clickable name="showTimecjgz" :value="formdata.workTime" label="参加工作时间"
              placeholder="请选择参加工作时间" @click="showTimecjgz = true" :rules="[{ required: true, message: ' ' }]" />
            <van-popup v-model="showTimecjgz" position="bottom">
              <van-datetime-picker type="date" v-model="currentDatework" :item-height="45" :maxDate="maxDate"
                :minDate="minDatework" @confirm="onConfirmtimecjgz" @cancel="showTimecjgz = false" />
            </van-popup>
            <van-field readonly clickable name="education" label="学历" :value="formdata.education" placeholder="请选择学历"
              @click="showPickerqwdd = true" :rules="[{ required: true, message: ' ' }]" />
            <van-popup v-model="showPickerqwdd" position="bottom">
              <van-picker show-toolbar value-key="name" :columns="columnsqwdd" :item-height="45"
                @confirm="onConfirmgqwdd" @cancel="showPickerqwdd = false" />
            </van-popup>
            <van-field v-model="formdata.graduateCollege" name="graduateCollege" label="毕业院校" placeholder="请填写毕业院校" />
            <van-field v-model="formdata.skillsSpecialties" name="skillsSpecialties" label="技能及特长"
              placeholder="请填写技能及特长" />
            <van-field readonly clickable name="tradeText" :value="formdata.tradeText" label="期望行业"
              placeholder="请选择期望行业" @click="tradetree(1)" :rules="[{ required: true, message: ' ' }]" />
            <van-field v-model="formdata.hopeSalary" name="hopeSalary" label="期望待遇" type="digit" placeholder="请输入期望待遇"
              :rules="[{ required: true, message: ' ' },
           ]" />
            <van-field readonly clickable name="majorCategoryText" :value="formdata.majorCategoryText" label="专业"
              placeholder="请选择专业" @click="majorCategorycl(2)" />
            <div style="margin: 16px;">
              <van-button round block type="info" style="width:8rem" native-type="submit">
                保存
              </van-button>
            </div>
          </van-form>

        </div>
      </div>
    </div>
    <div v-show="emplouxbhjdshow">
      <unemploucity ref="unemploucityone" v-on:positionon="positionon" @unmpoydel="unmpoydel"></unemploucity>
    </div>
    <div v-show="tradetreeshow">
      <unemploytread ref="unemploytreadtwo" @tradetreechren="tradetreechren" @tradetreefalse="tradetreefalse">
      </unemploytread>
    </div>
    <div v-show="unemploymjorshow">
      <unemploymjor ref="unemploymjorther" @majorCategoryclchren="majorCategoryclchren"
        @majorCategoryclfalse="majorCategoryclfalse"></unemploymjor>
    </div>
    <van-overlay :show="show" @click="show = false" />
  </div>
</template>
<script>
import UNEMPLOY from '@/api/unemploy'
import unemploucity from '@/views/unemploy/unemploucity'
import unemploytread from '@/views/unemploy/unemploytread'
import unemploymjor from '@/views/unemploy/unemploymjor'
export default {
  components: {
    unemploucity,
    unemploytread,
    unemploymjor
  },
  data () {
    return {
      emplouxb: false,
      enmopcityshow: true,
      emplouxbhjdshow: false,
      showTimecjgz: false,
      show: false,
      showPickerqwdd: false,
      tradetreeshow: false,
      unemploymjorshow: false,
      maxDate: new Date(),
      minDatework: new Date(1970, 0, 1),
      currentDatework: new Date(),
      columnsqwdd: [],
      trendtree: [],
      majorCategory: [],
      columnsxb: [
        {
          name: '男',
          val: 1
        },
        {
          name: '女',
          val: 2
        },
      ],
      formdata: {
        name: "",//姓名
        xb: '',
        sex: "",//性别
        idCard: "",//身份证
        sexCode: "",//性别编码
        domicilePlace: "",//户籍地
        domicilePlaceCode: "",//户籍地编码
        currentResidence: "",//现居地
        currentResidenceCode: '',//现居地编码
        workExp: "",//工作经验
        phone: "",//手机号
        mail: "",//邮箱
        workTime: "",//参加工作时间
        education: "",//学历
        educationCode: "",//学历编码
        graduateCollege: "",//别业院校
        skillsSpecialties: "",//技能及特长
        trade: null,//期望行业编码
        tradeText: "",//期望行业
        hopeSalary: "",//期望待遇
        majorCategoryText: "",//专业
        majorCategory: null,//专业编码
        id: '',
        domicilePlaceAddress: "",//户籍地地址
        currentResidenceAddress: ''//常住地地址
      }
    }
  },
  computed: {},
  methods: {

    //选择性别
    onConfirmxb (value) {
      this.formdata.sex = value.name
      console.log(value)
      this.formdata.sexCode = value.val
      this.emplouxb = false
    },
    emplouxbhjd (num) {
      this.enmopcityshow = false
      this.emplouxbhjdshow = true
      this.show = true
      this.$refs.unemploucityone.umplonum(num)
    },
    unmpoydel () {
      this.enmopcityshow = true
      this.emplouxbhjdshow = false
      this.show = false
    },
    //选择行业触发
    tradetree (num) {
      this.enmopcityshow = false
      this.tradetreeshow = true
      this.$refs.unemploytreadtwo.tcfindTree(this.trendtree, num, this.formdata.trade)
    },
    //关闭行业触发
    tradetreefalse () {
      this.enmopcityshow = true
      this.tradetreeshow = false
      console.log(44)
    },
    //行业子组件传过来的参数
    tradetreechren (num, val) {
      this.tradetreefalse()
      this.trendtree.map((item, i) => {
        item.childVo.map((atr, j) => {
          if (item.childVo[j].code === val.code) {
            this.formdata.tradeText = item.name + '-' + val.name
            this.formdata.trade = []
            this.formdata.trade.push(item.code)
            this.formdata.trade.push(val.code)
            this.formdata.trade = JSON.stringify(this.formdata.trade)
          }
        })
      })



    },
    //选择专业的时候触发
    majorCategorycl (num) {
      this.enmopcityshow = false
      this.unemploymjorshow = true
      this.$refs.unemploymjorther.tcfindTree(this.majorCategory, num, this.formdata.majorCategory)
    },
    //关闭专业触发
    majorCategoryclfalse () {
      this.enmopcityshow = true
      this.unemploymjorshow = false
    },
    //专业子组件传过来的参数
    majorCategoryclchren (num, val) {
      this.majorCategoryclfalse()

      this.majorCategory.map((item, i) => {
        item.childVo.map((atr, j) => {
          if (item.childVo[j].code === val.code) {
            this.formdata.majorCategoryText = item.name + '-' + val.name
            this.formdata.majorCategory = []
            this.formdata.majorCategory.push(item.code)
            this.formdata.majorCategory.push(val.code)
            this.formdata.majorCategory = JSON.stringify(this.formdata.majorCategory)
          }
        })
      })


    },
    //省市子组件传过来的数据
    positionon (code, name, num) {
      this.unmpoydel()
      console.log(num, 'num')
      if (num === 1) {
        this.formdata.domicilePlaceCode = JSON.stringify(code)
        this.formdata.domicilePlace = name.join('-')
        this.formdata.domicilePlaceAddress = name.join('')
      } else {
        this.formdata.currentResidenceCode = JSON.stringify(code)
        this.formdata.currentResidence = name.join('-')
        this.formdata.currentResidenceAddress = name.join('')
      }


    },
    // 参加工作时间选择
    onConfirmtimecjgz (date) {
      console.log(date)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.formdata.workTime = `${year}-${month}-${day}`
      this.showTimecjgz = false
    },
    // 选择学历
    onConfirmgqwdd (value) {
      this.formdata.education = value.name
      this.formdata.educationCode = value.val
      this.showPickerqwdd = false
    },
    // 获取字段表
    adminDict () {
      let that = this
      let params = 'education'
      UNEMPLOY.adminDict(params).then(res => {
        if (res.data.code === 200) {
          that.columnsqwdd = res.data.data

        }
      })
    },
    //获取行业
    findTradeDict () {
      UNEMPLOY.findTradeDict().then(res => {
        if (res.data.code === 200) {
          this.trendtree = res.data.data

        }
      })
    },
    //获取专业
    mcFindall () {
      UNEMPLOY.mcFindall().then(res => {
        if (res.data.code === 200) {
          this.majorCategory = res.data.data

        }
      })
    },
    back () {

      this.$router.go(-1)

    },

    init () {
      this.findTradeDict()
      this.mcFindall()
      this.adminDict()
    },
    // 工作经历新增修改
    onSubmit () {
      let params = this.formdata
      if (this.formdata.id === "") {
        let now = new Date();
        let year = now.getFullYear(); //得到年份
        let month = now.getMonth();//得到月份
        let date = now.getDate();//得到日期
        let hour = now.getHours();//得到小时
        let minu = now.getMinutes();//得到分钟
        let sec = now.getSeconds();//得到秒
        month = month + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        if (hour < 10) hour = "0" + hour;
        if (minu < 10) minu = "0" + minu;
        if (sec < 10) sec = "0" + sec;
        let timedata = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec
        params.createTime = timedata
        UNEMPLOY.resumeInsert(params).then(res => {
          this.buttondisabled = false
          console.log(res.data)
          if (res.data.success) {
            this.$toast('添加成功')
            this.$router.push({
              name: 'unemploy',
              query: {
                num: 1
              }
            })
          } else {
            this.$notify(res.data.msg)
          }
        })
      } else {
        UNEMPLOY.resumeUpdate(params).then(res => {
          this.buttondisabled = false
          console.log(res.data)
          if (res.data.success) {
            this.$toast('修改成功')
            this.$router.push({
              name: 'unemploy',
              query: {
                num: 1
              }
            })
          } else {
            this.$notify(res.data.msg)
          }
        })
      }

    },

    getDetail () {
      UNEMPLOY.loseJobDetail(this.$route.query.id).then(res => {
        if (res.data.success) {
          this.formdata = res.data.data || {};
        } else {
          this.$message.error(`${data.msg}`);
        }
      });
    },
  },
  created () {
    this.init()
    if (this.$route.query.id) {
      this.getDetail()
    }
  }
}
</script>
<style scoped>
.myumeremove .educationaddchild /deep/ .van-cell__title {
  font-weight: 400;
  text-align: left;
  margin: 0.24rem 0;
  width: 3rem;
}
.enplouheader {
  font-weight: bold;
}
.myumeremove .educationaddchild /deep/ .van-field__control {
  padding-right: 0;
}
</style>
